#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

struct Student {
    int id;
    char name[20];
    int age;
    double score;
    
    // 添加构造函数初始化成员
    Student() : id(0), age(0), score(0.0) {
        name[0] = '\0';
    }
};

class Io {
public:
    virtual ~Io() {}
    virtual int load(struct Student* p, int size) = 0;
    virtual int save(struct Student* p, int size) = 0;
};

class FileIo : public Io {
private:
    string filename;
public:
    FileIo(const string& fname) : filename(fname) {}
    
    int load(struct Student* p, int size) {
        ifstream file(filename.c_str(), ios::binary);
        if (!file) {
            cerr << "无法打开文件读取: " << filename << endl;
            return 0;
        }
        
        int count = 0;
        while (count < size && file.read((char*)&p[count], sizeof(Student))) {
            count++;
        }
        
        file.close();
        return count;
    }
    
    int save(struct Student* p, int size) {
        ofstream file(filename.c_str(), ios::binary);
        if (!file) {
            cerr << "无法打开文件写入: " << filename << endl;
            return 0;
        }
        
        for (int i = 0; i < size; i++) {
            file.write((char*)&p[i], sizeof(Student));
        }
        
        file.close();
        return size;
    }
};

class NormalIo : public Io {
private:
    Student* memoryBuffer;
    int bufferSize;
    int dataCount; // 记录实际存储的数据量
public:
    NormalIo(Student* buffer, int size) 
        : memoryBuffer(buffer), bufferSize(size), dataCount(0) {}
    
    int load(struct Student* p, int size) {
        int count = 0;
        while (count < size && count < dataCount) {
            p[count] = memoryBuffer[count];
            count++;
        }
        return count;
    }
    
    int save(struct Student* p, int size) {
        if (size > bufferSize) {
            cerr << "内存缓冲区空间不足" << endl;
            return 0;
        }
        
        for (int i = 0; i < size; i++) {
            memoryBuffer[i] = p[i];
        }
        dataCount = size;
        return size;
    }
};

class StudentManagementSystem {
private:
    static const int MAX_STUDENTS = 100;
    Student students[MAX_STUDENTS];
    int studentCount;
    Io* ioHandler;
    
public:
    StudentManagementSystem(Io* handler) : ioHandler(handler), studentCount(0) {}
    
    void loadData() {
        studentCount = ioHandler->load(students, MAX_STUDENTS);
        cout << "加载了 " << studentCount << " 条学生记录" << endl;
    }
    
    void saveData() {
        int saved = ioHandler->save(students, studentCount);
        cout << "保存了 " << saved << " 条学生记录" << endl;
    }
    
    void addStudent(int id, const char* name, int age, double score) {
        if (studentCount >= MAX_STUDENTS) {
            cout << "学生数量已达上限!" << endl;
            return;
        }
        
        students[studentCount].id = id;
        strncpy(students[studentCount].name, name, 19);
        students[studentCount].name[19] = '\0';
        students[studentCount].age = age;
        students[studentCount].score = score;
        studentCount++;
    }
    
    void displayAll() {
        if (studentCount == 0) {
            cout << "没有学生记录" << endl;
            return;
        }
        
        cout << "ID\tName\tAge\tScore" << endl;
        cout << "---------------------------" << endl;
        for (int i = 0; i < studentCount; i++) {
            cout << students[i].id << "\t" 
                 << students[i].name << "\t" 
                 << students[i].age << "\t" 
                 << students[i].score << endl;
        }
    }
    
    void findById(int id) {
        for (int i = 0; i < studentCount; i++) {
            if (students[i].id == id) {
                cout << "找到学生: " << endl;
                cout << "ID: " << students[i].id << endl;
                cout << "姓名: " << students[i].name << endl;
                cout << "年龄: " << students[i].age << endl;
                cout << "分数: " << students[i].score << endl;
                return;
            }
        }
        cout << "未找到ID为 " << id << " 的学生" << endl;
    }
};

int main() {
    // 使用文件IO
    FileIo fileIo("students.dat");
    StudentManagementSystem fileSystem(&fileIo);
    
    // 添加一些学生数据
    fileSystem.addStudent(1, "张三", 20, 85.5);
    fileSystem.addStudent(2, "李四", 21, 90.0);
    fileSystem.addStudent(3, "王五", 19, 78.5);
    
    // 保存到文件
    fileSystem.saveData();
    
    // 显示所有学生
    fileSystem.displayAll();
    
    // 使用内存IO
    Student memoryBuffer[100];
    NormalIo normalIo(memoryBuffer, 100);
    StudentManagementSystem memorySystem(&normalIo);
    
    // 从内存加载数据（此时应该是空的）
    memorySystem.loadData();
    
    // 在内存系统中添加新学生
    memorySystem.addStudent(4, "赵六", 22, 92.5);
    memorySystem.addStudent(5, "钱七", 20, 88.0);
    
    // 显示内存系统中的学生
    memorySystem.displayAll();
    
    // 保存到内存
    memorySystem.saveData();
    
    return 0;
}